package com.demo.my;

import java.io.UnsupportedEncodingException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.his.emr.print.utils.MyDriver;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;

/**
 * date: 2018年2月15日,
 * 参考 <a href="http://commons.apache.org/proper/commons-vfs/filesystems.html">http://commons.apache.org/proper/commons-vfs/filesystems.html</a><br> "ram": `ram://[ path]`<br>
 * 
 * {@link RamController#personate()} 和 {@link RamController#test()} : 测试表明下面的方法无效,不能使用这种方式在内存中访问html.<br>
 * 其实,此刻静心思考下就发现,"phantomjs.exe"不能访问java内存。但是我是使用http传输的啊,这样想的话与java内存无关!<br>
 * 总之,在内存中完成WebDriver.get(url)的调用是行不通了!<br>
 * 
 * @author Wxg
 *
 */
public class RamController extends Controller{
	
	public void personate(){
		String name = getPara("personateKey");
		// Object obj = getSession().getAttribute(name);
		// JFinal.me().getServletContext().setAttribute("", "");
		Object obj = JFinal.me().getServletContext().getAttribute(name);
		
		renderHtml(obj.toString());
		
	}
	
	public void test(){
		try {
			getRequest().setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String htmlStr = getPara("htmlStr");
		
		JFinal.me().getServletContext().setAttribute("htmlStr", htmlStr);
		
		WebDriver driver = MyDriver.getStaticDriver();
		String url = null;
		url = getRequest().getContextPath() + "/ram/personate?personateKey=htmlStr";
		driver.get(url);
		
		WebElement body = driver.findElement(By.tagName("body"));
		if(null != body){
			System.out.println( body.getAttribute("outerHTML") );
		}else{
			System.out.println("=== body is null...");
		}
	}
	
}
